<?php
    class loaisanphamc1db
    {
        public $_tablename = "giaphucvn_loaisanphamcap1";
        public function getList($MaLoaiSanPhamCap1 = 0, $TenLoaiSanPhamCap1 = '', $MaLoaiSanPham = 0,$order = '', $start = 0, $num = 30, $all = 0)
		{
			$sql = "SELECT * FROM ".$this->_tablename." WHERE (1)";
			
			if ($MaLoaiSanPhamCap1 != 0)
				$sql .= " AND MaLoaiSanPhamCap1 = " . dataservice::QuoteSmart($MaLoaiSanPhamCap1);
			
			if ($TenLoaiSanPhamCap1 != '')
				$sql .= " AND TenLoaiSanPhamCap1 LIKE " . dataservice::QuoteSmart('%' . $TenLoaiSanPhamCap1 . '%');
                
            if ($MaLoaiSanPham != 0)
				$sql .= " AND MaLoaiSanPham = " . dataservice::QuoteSmart($MaLoaiSanPham);
			
			if ($order != '')
				$sql .= " ORDER BY " . $order . " DESC";
			
			if($all == 0)
				$sql .= " LIMIT " . $start . ", ".$num;
			//echo $sql;
			$db = new dataservice();
			$ret = $db->ExecuteQuery($sql);
			return $ret;
		}
        
        public function getIds($MaLoaiSanPham = 0, $MaLoaiSanPhamCap2 = 0, $MaLoaiSanPhamCap3 = 0)
		{
			$sql = "SELECT giaphucvn_loaisanphamcap1.MaLoaiSanPhamCap1 FROM ";
			$sql .= "giaphucvn_loaisanphamcap3 join giaphucvn_loaisanphamcap2 on giaphucvn_loaisanphamcap3.MaLoaiSanPhamCap2 = giaphucvn_loaisanphamcap2.MaLoaiSanPhamCap2 
            join giaphucvn_loaisanphamcap1 on giaphucvn_loaisanphamcap2.MaLoaiSanPhamCap1 = giaphucvn_loaisanphamcap1.MaLoaiSanPhamCap1 
            join giaphucvn_loaisanpham on giaphucvn_loaisanphamcap1.MaLoaiSanPham = giaphucvn_loaisanpham.MaLoaiSanPham WHERE (1)";
			if ($MaLoaiSanPham != 0)
				$sql .= " AND giaphucvn_loaisanpham.MaLoaiSanPham = " . dataservice::QuoteSmart($MaLoaiSanPham);
			
			if ($MaLoaiSanPhamCap2 != 0)
				$sql .= " AND giaphucvn_loaisanphamcap2.MaLoaiSanPhamCap2 = " . dataservice::QuoteSmart($MaLoaiSanPhamCap2);
			
			if ($MaLoaiSanPhamCap3 != 0)
				$sql .= " AND giaphucvn_loaisanphamcap3.MaLoaiSanPhamCap3 = " . dataservice::QuoteSmart($MaLoaiSanPhamCap3);
                
			//echo $sql;
			$db = new dataservice();
			$ret = $db->ExecuteQuery($sql);
			return $ret;
		}
		
		public function add($TenLoaiSanPhamCap1 = '', $MaLoaiSanPham = 0)
		{
			$sql = "INSERT INTO ".$this->_tablename."(TenLoaiSanPhamCap1, MaLoaiSanPham) VALUES(" . dataservice::QuoteSmart($TenLoaiSanPhamCap1).", ". dataservice::QuoteSmart($MaLoaiSanPham) . ")";
			
			$db = new dataservice();
			$ret = $db->ExecuteNonQuery($sql);
			return $ret;
		}
		
		public function update($MaLoaiSanPhamCap1 = 0, $TenLoaiSanPhamCap1 = '', $MaLoaiSanPham = 0)
		{
			$sql  = "UPDATE ".$this->_tablename." ";
            $sql .= "SET TenLoaiSanPhamCap1 = (" . dataservice::QuoteSmart($TenLoaiSanPhamCap1) . "), ";
            $sql .= "MaLoaiSanPham = ". dataservice::QuoteSmart($MaLoaiSanPham);
            $sql .= " WHERE MaLoaiSanPhamCap1 = ".dataservice::QuoteSmart($MaLoaiSanPhamCap1);
			
			$db = new dataservice();
			$ret = $db->ExecuteNonQuery($sql);
			return $ret;
		}
		
		public function delete($MaLoaiSanPhamCap1 = 0)
		{
			$sql = "DELETE FROM ".$this->_tablename." WHERE (1)";
			
			if ($MaLoaiSanPhamCap1 != 0)
				$sql .= " AND MaLoaiSanPhamCap1 = " . dataservice::QuoteSmart($MaLoaiSanPhamCap1);
			
			$db = new dataservice();
			return $db->ExecuteNonQuery($sql);
		}
        
        public function issetTypes($MaLoaiSanPhamCap1 = 0, $TenLoaiSanPhamCap1 = "", $MaLoaiSanPham = 0)
		{
			$sql = "SELECT COUNT(*) AS SoDong FROM ".$this->_tablename." WHERE (1)";
            
            if($MaLoaiSanPhamCap1 != 0)
				$sql .= " AND MaLoaiSanPhamCap1 = $MaLoaiSanPhamCap1";
            
			if($TenLoaiSanPhamCap1 != "")
				$sql .= " OR TenLoaiSanPhamCap1 = ". dataservice::QuoteSmart($TenLoaiSanPhamCap1);
                
            if($MaLoaiSanPham != 0)
				$sql .= " AND MaLoaiSanPham = $MaLoaiSanPham";
			//echo $sql;
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
        
        public function issetType($TenLoaiSanPhamCap1 = "", $MaLoaiSanPham = 0)
		{
			$sql = "SELECT COUNT(*) AS SoDong FROM ".$this->_tablename." WHERE (1)";
            
			if($TenLoaiSanPhamCap1 != "")
				$sql .= " AND TenLoaiSanPhamCap1 = " . dataservice::QuoteSmart($TenLoaiSanPhamCap1);
                
			if($MaLoaiSanPham != 0)
				$sql .= " AND MaLoaiSanPham = " . dataservice::QuoteSmart($MaLoaiSanPham);
                
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
        
        public function totalPage($SoLoaiSanPham = 0)
        {
            $sql  = "SELECT * FROM ".$this->_tablename." ";
            $sql .= "WHERE (1)";
                
            $data = new dataservice();
            $tongsodong = $data->ReturnNumRows($sql);
            $tongsotrang = ceil($tongsodong / $SoLoaiSanPham);
            return $tongsotrang;
        }
        
        public function paging($SoLoaiSanPham = 1, $P = 1)
        {
            $x = ($P-1) * $SoLoaiSanPham;
            $sql  = "SELECT * FROM ".$this->_tablename." ";
            $sql .= "WHERE (1)";
                
            $sql .= " LIMIT $x,$SoLoaiSanPham";
            $data = new dataservice();
            return $data->ExecuteQuery($sql);
        }
    }
?>